const tf = require('@tensorflow/tfjs-node');
const fs = require('fs-extra');
const className = require('./model/type.json')

async function predict() {
    // 加载训练好的模型
    const modelPath = 'file://model/model.json';
    const model = await tf.loadLayersModel(modelPath);
    
    // 加载待预测的图像
    // const imageBuffer = fs.readFileSync('../image/test/daisy/22873310415_3a5674ec10_m.jpg');
    const tfimage = tf.node.decodeImage(fs.readFileSync('../image/test/dandelion/34310456510_a4ceda64da_n.jpg'))
    .resizeNearestNeighbor([96, 96])
    .toFloat()
    .div(tf.scalar(255.0))
    .expandDims()
    // const tfimage = tf.node.decodeImage(imageBuffer);
    
    // 进行预测
    const predictions = model.predict(tfimage);
    const predictedClassIndex = predictions.argMax(1).dataSync()[0];
    console.log('predictedClassIndex', predictedClassIndex, className)
    console.log(className[predictedClassIndex])
    // 输出预测结果
    predictions.print();
}
predict()